package com.shenrongrong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenrongrong.mapper.GoodsMapper;
import com.shenrongrong.model.Goods;
import com.shenrongrong.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.map.MapUtil.getDate;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional
    public PageInfo<Goods> selectAllGoods(Integer pageNum,Integer pageSize,Goods goods) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = goodsMapper.selectAllGoods(goods);
        return new PageInfo<Goods>(goodsList);
    }

    @Override
    public List<Goods> selectTipGoods(Integer userid) {
        Goods goods = new Goods();
        goods.setNumber(1);
        goods.setDegree(3);
        goods.setEndtime(LocalDate.now().plusDays(2));
        goods.setUserid(userid);
        return goodsMapper.selectAllGoods(goods);
    }

    @Override
    public int insertGoods(Goods goods) {
        return goodsMapper.insertGoods(goods);
    }
    @Override
    public boolean isgoodUnique(String name,String location,Integer goodid){
        Goods goods=new Goods();
        goods.setName(name);
        goods.setLocation(location);
        goods.setGoodid(goodid);
        if(goodsMapper.selectAllGoods(goods).size() == 1){
            return false;
        }
        return true;
    }

    @Override
    public int deleteGoods(Integer goodid) {
        return goodsMapper.deleteGoods(goodid);
    }

    @Override
    public List<Map<String,String>> selectByClassification(Integer userid) {
        return goodsMapper.selectByClassification(userid);
    }

    @Override
    public int updateGoodsNumber(Integer goodid,Integer number) {
        return goodsMapper.updateGoodsNumber(goodid,number);
    }

    @Override
    public int updateGoods(Goods goods) {
        return goodsMapper.updateGoods(goods);
    }


}
